package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.WxOrderCommentBo;
import com.cskaoyan.bean.bo.WxOrderIdBo;
import com.cskaoyan.bean.bo.WxOrderListReqBo;
import com.cskaoyan.bean.bo.WxOrderSubmitBo;
import com.cskaoyan.bean.entity.OrderGoods;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.WxOrderDetailVo;
import com.cskaoyan.bean.vo.WxOrderSubmitVo;
import com.cskaoyan.exception.OrderSubmitException;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.utils.BaseRespVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author break_a_leg
 * @description
 * @date 2021年10月21日 16:16
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("list")
    public BaseRespVo list(WxOrderListReqBo wxOrderListReqBo) {
        BasePageData data = orderService.queryList(wxOrderListReqBo);
        if (data == null) {
            return BaseRespVo.fail("订单列表为空");
        }
        return BaseRespVo.ok(data);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WxOrderDetailVo wxOrderDetailVo = orderService.queryOrderDetail(orderId);
        if (wxOrderDetailVo == null) {
            return BaseRespVo.fail("找不到该订单信息");
        }
        return BaseRespVo.ok(wxOrderDetailVo);
    }

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody WxOrderCommentBo wxOrderCommentBo) {
        int code = orderService.insertComment(wxOrderCommentBo);
        return BaseRespVoUtil.respByConstantCode(code,"评论发表失败");
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody WxOrderIdBo orderId) {
        int code = orderService.updateOrderCancel(orderId);
        return BaseRespVoUtil.respByConstantCode(code,"订单取消失败");
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody WxOrderIdBo wxOrderIdBo) {
        int code = orderService.updateOrderRefund(wxOrderIdBo);
        return BaseRespVoUtil.respByConstantCode(code,"无法申请退款");
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody WxOrderIdBo wxOrderIdBo) {
        int code = orderService.updateOrderDeleted(wxOrderIdBo);
        return BaseRespVoUtil.respByConstantCode(code,"订单删除失败");
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody WxOrderIdBo wxOrderIdBo) {
        int code = orderService.updateOrderConfirm(wxOrderIdBo);
        return BaseRespVoUtil.respByConstantCode(code,"无法确认收货");
    }

    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId,Integer goodsId) {
        OrderGoods orderGoods = orderService.queryOrderGoods(orderId,goodsId);
        if (orderGoods == null) {
            BaseRespVo.fail("找不到该商品信息");
        }
        return BaseRespVo.ok(orderGoods);
    }

    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody WxOrderSubmitBo wxOrderSubmitBo) throws OrderSubmitException {
        WxOrderSubmitVo data = orderService.insertOrder(wxOrderSubmitBo);
        return BaseRespVo.ok(data);
    }
}
